//
//  XQAccountWalletCell.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/27.
//

#import "XQAccountWalletCell.h"

@interface XQAccountWalletCell ()
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIImageView *leftImageView;
@property (nonatomic,strong) UILabel *leftLb;
@property (nonatomic,strong) UILabel *rightLb;
@property (nonatomic,strong) UIButton *leftButton;
@property (nonatomic,strong) UIButton *rightButton;
@end

@implementation XQAccountWalletCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = kCardColor;
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.leftImageView];
    [self.bgView addSubview:self.leftLb];
    [self.bgView addSubview:self.rightLb];
    [self.bgView addSubview:self.leftButton];
    [self.bgView addSubview:self.rightButton];
}

- (void)makeConstraints {
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@-20);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(@0);
    }];
    
    [self.leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@56);
        (void)make.centerY;
    }];
    [self.rightLb mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerY;
        make.right.equalTo(@-62);
    }];
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(@0);
        make.width.mas_equalTo(([UIScreen mainScreen].width - 24) / 2);
    }];
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(@0);
        make.left.equalTo(self.leftButton.mas_right);
    }];
}

#pragma mark - setter

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    self.bgView.backgroundColor = selectIndex == 0 ? [UIColor whiteColor] : kBlueTextColor;
    self.leftImageView.image = [UIImage imageNamed:selectIndex == 0 ? @"wallet" : @"bank"];
    self.leftLb.textColor = selectIndex == 0 ? [UIColor whiteColor] : k999999Color;
    self.rightLb.textColor = selectIndex == 1 ? [UIColor whiteColor] : k999999Color;
    self.leftLb.font = selectIndex == 0 ? [UIFont boldSystemFontOfSize:17] : [UIFont systemFontOfSize:17];
    self.rightLb.font = selectIndex == 1 ? [UIFont boldSystemFontOfSize:17] : [UIFont systemFontOfSize:17];
}

- (void)setTitles:(NSArray *)titles {
    for (int i = 0; i < titles.count; i++) {
        if (i == 0) {
            self.leftLb.text = titles[i][@"chance"];
        }else {
            self.rightLb.text = titles[i][@"chance"];
        }
    }
    
}

#pragma mark - private

- (void)leftClick {
    self.selectIndex = 0;
    
    if (self.buttonHandle) {
        self.buttonHandle(self.leftLb.text);
    }
    
}

- (void)rightClick {
    self.selectIndex = 1;
    
    if (self.buttonHandle) {
        self.buttonHandle(self.rightLb.text);
    }
}

#pragma mark - lazy

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.layer.cornerRadius = 15;
    }
    return _bgView;
}

- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] init];
        [_leftImageView sizeToFit];
    }
    return _leftImageView;;
}

- (UILabel *)leftLb {
    if (!_leftLb) {
        _leftLb = [[UILabel alloc] init];
    }
    return _leftLb;
}

- (UILabel *)rightLb {
    if (!_rightLb) {
        _rightLb = [[UILabel alloc] init];
    }
    return _rightLb;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton addTarget:self action:@selector(leftClick) forControlEvents: UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton addTarget:self action:@selector(rightClick) forControlEvents: UIControlEventTouchUpInside];
    }
    return _rightButton;
}

@end
